// store.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollDown: false,
    hiddenStatus: false,
    storeInfos:{},
    storeRecommend:{},
    allGoods:{},
    storenavbar: ['店铺首页', '全部产品页'],
    currentTab: 0,
    navLeftItems: {},
    navRightItems: {},
    curNav: 1,
    curIndex: 0,
    userInfo:{},
    storeUrls: [
      'https://xcx.rongbaiwei.com/public/upload/xcx/store/store1.png',
      'https://xcx.rongbaiwei.com/public/upload/xcx/store/store1.png'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  //左侧tab栏事件  
  switchRightTab: function (e) {
    let that = this
    var id = e.target.dataset.id
    var shopId = that.data.shop_id
    this.setData({
      curNav: id
    })
    //全部产品分类接口
    util.request(api.StoreAllGoods, { shop_id: shopId, shop_type: id}).then(function(res){
      
      if(res.data.code == 200){
        that.setData({
          navRightItems: res.data.data.goods
        })
      }
    })

  },
  //顶部滚动事件
  onScroll:function (e) {
    if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true,
        hiddenStatus:true
      });
    } else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
      this.setData({
        scrollDown: false,
        hiddenStatus: false
      });
    }
  },
  //店铺首页商品店铺信息
  store: function () {
    let that = this
    util.request(api.Store, { shop_id: that.data.shop_id }).then(function (res) {
      
      if (res.data.code == 200) {
        that.setData({
          storeInfos: res.data.data.shopInfo,
          storeRecommend: res.data.data.recommend
        })
      }
    })

  },
  //店铺全部产品页信息
  storeallgoods: function () {
    let that = this
    util.request(api.StoreAllGoods, { shop_id: that.data.shop_id, shop_type: 0}).then(function (res) {
      
      if (res.data.code == 200) {
        that.setData({
          navLeftItems: res.data.data.gcate,
          navRightItems: res.data.data.goods
        })
      }
    })

  },
  //点击支付,弹出支付框
  payFor:function(){
    var userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      let that = this
      var shop_id = that.data.shop_id
      wx.navigateTo({
        url: 'pay/pay?sp_id=' + shop_id
      }) 
    }else{
      wx.showModal({
        title: '',
        content: '您还没有去登陆，请先登录!',
        success:function(res){
          if(res.confirm){
            wx.switchTab({
              url: '/pages/admin/admin'
            });
          } 
        }
      })
    }
     
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shop_id: parseInt(options.shop_id)
    })
    this.store()
    this.storeallgoods()
  },
  
})